package com.hanlink.dsp.ui.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;


import com.edge.pbxdsp.R;
import com.hanlink.dsp.bluetooth.ble.BtDevice;
import com.qztech.utils.YzDebug;

import java.util.List;

/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C): 2015/10/15
 *
 * Description:
 *
 * Revision History
 * Date			  Author		Description
 * 2015/10/15        PeterVon       初始创建
 *
 * </pre>
 */
public class DeviceAdapter extends BaseAdapter {

    private Context mContext;

    private List<BtDevice> mItems;
    private Resources mResources;
    public DeviceAdapter(Context context, List<BtDevice> items) {
        mContext = context;
        mItems = items;
        mResources = context.getResources();
    }

    @Override
    public int getCount() {
        return mItems == null ? 0 : mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems == null ? null : mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        BtDevice device = (BtDevice) getItem(position);
        YzDebug.d("getView:"+ device);
        DeviceViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(com.hanlink.res.R.layout.list_item_device, parent, false);
            holder = new DeviceViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (DeviceViewHolder) convertView.getTag();
        }
        holder.ivDevice.setBackgroundResource(com.hanlink.res.R.drawable.icon_bluetooth);
        holder.txtName.setText(device.getName());
        holder.txtAddr.setText(device.getMac());
        if (device.isConnected()) {
            holder.txtName.setTextColor(getColor(com.hanlink.res.R.color.bg_checked));
            holder.txtAddr.setTextColor(getColor(com.hanlink.res.R.color.bg_checked));
            holder.switchBar.setTag(true);
            holder.switchBar.setImageResource(com.hanlink.res.R.drawable.switch_on);
        } else {
            holder.txtName.setTextColor(getColor( com.hanlink.res.R.color.common_tab_bg));
            holder.txtAddr.setTextColor(getColor( com.hanlink.res.R.color.common_tab_bg));
            holder.switchBar.setTag(false);
            holder.switchBar.setImageResource(com.hanlink.res.R.drawable.switch_off);
        }
        holder.switchBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = !(boolean) v.getTag();
                if (onSwichChangedListener != null) {
                    onSwichChangedListener.changed(isChecked, device);
                }
            }
        });
        return convertView;
    }

    private @ColorInt int getColor(@ColorRes int colorId){
        return mResources.getColor(colorId);
    }
    private OnSwichChangedListener onSwichChangedListener;

    public void setOnSwichChangedListener(OnSwichChangedListener onSwichChangedListener) {
        this.onSwichChangedListener = onSwichChangedListener;
    }

    public interface OnSwichChangedListener {
        void changed( boolean isChecked, BtDevice btDevice);
    }

    class DeviceViewHolder {
        ImageView ivDevice;
        TextView txtName;
        TextView txtAddr;
        ImageButton ibtnSettings;
        ImageButton switchBar;

        public DeviceViewHolder(View view) {
            ivDevice = (ImageView) view.findViewById(R.id.ivDevice);
            txtName = (TextView) view.findViewById(R.id.txtName);
            txtAddr = (TextView) view.findViewById(R.id.txtAddr);
            ibtnSettings = (ImageButton) view.findViewById(R.id.ibtnSettings);
            switchBar = view.findViewById(R.id.switchBar);
        }

    }
}
